package com.zs.blog.service.impl;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.blog.constant.enums.ResponseResultEnum;
import com.zs.blog.converter.CategoryConverter;
import com.zs.blog.dao.IArticleMapper;
import com.zs.blog.dao.ICategoryMapper;
import com.zs.blog.exception.BusinessException;
import com.zs.blog.model.ResponseResult;
import com.zs.blog.model.dto.ArticleCategoryDTO;
import com.zs.blog.model.dto.CategoryDTO;
import com.zs.blog.model.dto.CategoryEditDTO;
import com.zs.blog.model.dto.CategorySearchDTO;
import com.zs.blog.model.entity.Category;
import com.zs.blog.model.vo.CategoryEditVO;
import com.zs.blog.model.vo.CategoryListVO;
import com.zs.blog.model.vo.PageInfoVO;
import com.zs.blog.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Title: ICategoryServiceImpl
 * @Author zs
 * @Package com.zs.blog.service.impl
 * @Date 2024/10/20 16:11
 * @description: 分类服务层接口实现类
 */
@Service
public class ICategoryServiceImpl extends ServiceImpl<ICategoryMapper, Category> implements ICategoryService {

    public ICategoryMapper categoryMapper;
    public IArticleMapper articleMapper;


    @Autowired
    public ICategoryServiceImpl(ICategoryMapper categoryMapper, IArticleMapper articleMapper) {
        this.categoryMapper = categoryMapper;
        this.articleMapper = articleMapper;
    }

    @Override
    public PageInfoVO<CategoryListVO> listCategoryWithPagination(Integer pageNum, Integer pageSize) {
        Page<CategoryDTO> page = new Page<>(pageNum, pageSize);
        List<CategoryDTO> categoryDTOList = categoryMapper.listCategoryWithPagination(page);

        List<CategoryListVO> categoryListVOList = CategoryConverter.INSTANCE.categoryDto2CategoryVo(categoryDTOList);

        PageInfoVO<CategoryListVO> pageInfoVO = new PageInfoVO<>();
        pageInfoVO.setTotal(page.getTotal());
        pageInfoVO.setItems(categoryListVOList);

        return pageInfoVO;
    }

    @Override
    public PageInfoVO<CategoryListVO> listCategoryBySearchCondition(CategorySearchDTO categorySearchDTO) {
        Page<CategoryDTO> page = new Page<>(categorySearchDTO.getPageNum(), categorySearchDTO.getPageSize());

//        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), CategorySearchDTO.class);

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>(Category.class);
        lqw.eq(StringUtils.isNotBlank(categorySearchDTO.getCategoryName()), Category::getCategoryName, categorySearchDTO.getCategoryName())
                .eq(categorySearchDTO.getIsDeleted() != null && StringUtils.isNotBlank(categorySearchDTO.getIsDeleted().toString()), Category::getIsDeleted, categorySearchDTO.getIsDeleted());
        if (StringUtils.isNotBlank(categorySearchDTO.getStartTime().toString()) && StringUtils.isNotBlank(categorySearchDTO.getEndTime().toString())) {
            lqw.between(Category::getCreateTime, categorySearchDTO.getStartTime(), categorySearchDTO.getEndTime());
        }

        List<CategoryDTO> categoryDTOList = categoryMapper.listCategoryBySearchCondition(page, lqw);

        List<CategoryListVO> categoryListVOList = CategoryConverter.INSTANCE.categoryDto2CategoryVo(categoryDTOList);

        PageInfoVO<CategoryListVO> pageInfoVO = new PageInfoVO<>();
        pageInfoVO.setTotal(page.getTotal());
        pageInfoVO.setItems(categoryListVOList);

        return pageInfoVO;
    }

    /**
     * 根据分类ID删除分类
     *
     * @param categoryIdList 分类ID列表
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<?> deleteCategoryByIds(List<Integer> categoryIdList) {
        // 分类下有文章则不能删除
        Integer count = categoryMapper.getArticleCountUnderCategory(categoryIdList);
        if (count > 0) {
            return ResponseResult.failure(ResponseResultEnum.CATEGORY_EXIT_ARTICLE);
        }

        categoryMapper.deleteCategoryByIds(categoryIdList);
        return ResponseResult.success();
    }

    /**
     * 新增分类
     *
     * @param categoryEditDTO 分类编辑DTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdateCategory(CategoryEditDTO categoryEditDTO) {
        // 判断分类名称是否相同
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getCategoryName, categoryEditDTO.getCategoryName());
        Category category = categoryMapper.selectOne(lqw);
        if (Objects.nonNull(category)) {
            throw new BusinessException("分类名称已经存在");
        }

        Category c = CategoryConverter.INSTANCE.categoryEditDto2Category(categoryEditDTO);
        categoryMapper.insertOrUpdate(c);
    }

    @Override
    public CategoryEditVO getCategoryByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getId, categoryId);
        Category category = categoryMapper.selectOne(lqw);

        return CategoryConverter.INSTANCE.category2CategoryEditVO(category);
    }

    @Override
    public List<CategoryListVO> listCategory() {
        return categoryMapper.listCategory();
    }

}
